home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Simulation / PDP-8 Simulator / Source Code / EdWindows.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-13  |  12.8 KB  |  547 lines  |  [TEXT/KAHL]

  1. /*************************************************************************************
  2. *
  3. *        EdWindows.c    Defines types and code for generic text editor windows
  4. *
  5. *        Modification History:
  6. *        18/2/92 created from scratch.    
  7. *
  8. *
  9. *
  10. *************************************************************************************/
  11. #include    "EditGlobalEqu.p"
  12.  
  13. #include "EdWindows.proto.h"
  14.  
  15.  
  16. textEdHdl NewEditRecord(WindowPtr ownerWindow);
  17.  
  18.  
  19.  
  20. char    IsEditKind(WindowPtr theWindow)
  21. {
  22.     /* returns TRUE if window is editor kind */
  23.     
  24.     if (theWindow != NIL)
  25.         return((*(WindowPeek)theWindow).windowKind == EditWindowKind);
  26.     else
  27.         return(FALSE);
  28. }
  29.  
  30.  
  31. textEdHdl GetWEditRecord(WindowPtr theWindow)
  32. {
  33.     /* returns handle to window's edit record, if window is editkind */
  34.     if (IsEditKind(theWindow))
  35.         return ((textEdHdl)GetWRefCon(theWindow));    
  36. }
  37.  
  38.  
  39. DrawEditWindow(WindowPtr theWindow)
  40. {
  41.     /* call for edit type window to draw the window called from update event so
  42.         is current port and visRgn set up  */
  43.     
  44.     DrawTabBar(theWindow,NIL);
  45.     SetWindowClip(theWindow);
  46.     DrawList(theWindow,NIL);
  47.     UnClipWindow(theWindow);
  48.     DrawControls(theWindow);
  49.     DrawGrowIcon(theWindow);
  50. }
  51.  
  52.  
  53. DrawTabBar(WindowPtr theWindow,RgnHandle updateRgn)
  54. {
  55.     /* draws tab bar for the edit window- at present just draws delimiting line */
  56.     
  57.     Rect             tr,mr;
  58.     CTabHandle        tbColours;
  59.     OSErr            theErr;
  60.     SysEnvRec        macEnv;
  61.     int                index,saveFont,saveSize,chCnt;
  62.     int                hOrigin,charPix,tH,isColour;
  63.     RgnHandle        saveClip;
  64.     Str32            rulerText;
  65.     textEdHdl        theText;
  66.     GDHandle        theDevice;
  67.     
  68.     saveClip = NewRgn();
  69.     GetClip(saveClip);
  70.     theText = GetWEditRecord(theWindow);
  71.     if (theText != NIL) {
  72.         hOrigin = GetMarginOffset(theText);
  73.         charPix = (*theText)->mSpace;
  74.         tH = GetTabBarHeight(theText);
  75.     }
  76.     
  77.     if (tH != 0) {                        
  78.     
  79.         SetRect(&tr,thePort->portRect.left,thePort->portRect.top
  80.                     ,thePort->portRect.right-15,thePort->portRect.top + tH-2);
  81.         
  82.         if (updateRgn != NIL)
  83.             SetClip(updateRgn);
  84.         else
  85.             ClipRect(&tr);
  86.         
  87.         isColour = FALSE;
  88.         theErr = SysEnvirons(2,&macEnv);
  89.         if (theErr == noErr && macEnv.hasColorQD) {
  90.             mr = tr;
  91.             LocalToGlobal(&topLeft(mr));
  92.             LocalToGlobal(&botRight(mr));
  93.             theDevice = GetMaxDevice(&mr);
  94.             if (theDevice != NIL )
  95.                 isColour = (**(*theDevice)->gdPMap).pixelSize > 2;    
  96.         
  97.         }
  98.             
  99.         if (isColour) {
  100.             tbColours = GetCTable(standardResID);
  101.             if (tbColours != NIL) {
  102.                 RGBForeColor(&(*tbColours)->ctTable[1].rgb);
  103.                 PaintRect(&tr);
  104.                 ForeColor(blackColor);
  105.                 DisposeCTable(tbColours);
  106.             }
  107.         }
  108.     
  109.         saveFont = thePort->txFont;
  110.         saveSize = thePort->txSize;
  111.         TextFont(geneva);
  112.         TextSize(9);
  113.         MoveTo(thePort->portRect.left + hOrigin,thePort->portRect.top+10);
  114.         
  115.         for (index =0;index<(charPix * 256);index += charPix) {
  116.             if ((index % 20) == 0) {
  117.                 NumToString(index/charPix,&rulerText);
  118.                 Move(-(StringWidth(&rulerText)/2),-1);
  119.                 DrawString(&rulerText);
  120.                 Move(-(StringWidth(&rulerText)/2),1);
  121.                 PenSize(2,1);
  122.             }
  123.             Line(0,5);
  124.             PenNormal();
  125.             Line(charPix,0);
  126.             Move(0,-5);
  127.         }
  128.         /*DrawTabMarkers(theText,hOrigin);*/
  129.         SetClip(saveClip);
  130.         TextFont(saveFont);
  131.         TextSize(saveSize);
  132.         MoveTo(thePort->portRect.left,thePort->portRect.top + tH -2);
  133.         LineTo(thePort->portRect.right-15,thePort->portRect.top+ tH -2);
  134.         MoveTo(thePort->portRect.left,thePort->portRect.top+ tH);
  135.         LineTo(thePort->portRect.right-15,thePort->portRect.top+ tH);
  136.     }
  137.     DisposeRgn(saveClip);
  138. }
  139.  
  140.  
  141. DrawTabMarkers(textEdHdl theText,int margin)
  142. {
  143.     /* draws tab markers. */
  144.  
  145.     int        i,tabStop;
  146.     
  147.     MoveTo(thePort->portRect.left + margin,thePort->portRect.top+20);
  148.     if (theText != NIL) {
  149.         for(i=0;i<10;i++){
  150.             tabStop = (*theText)->tabs[i];
  151.             Move(tabStop * (*theText)->mSpace,0);
  152.             Line(0,6);
  153.             Move(-tabStop * (*theText)->mSpace,-6);
  154.         }
  155.     }
  156. }
  157.  
  158. InitEditWindow(WindowPtr theWindow)
  159. {
  160.     /* initialises editor window, when window created. Sets up scrollbars etc */
  161.     
  162.     Rect            r,wPort;
  163.     ControlHandle     sBar;
  164.     textEdHdl        wEditRec;
  165.     
  166.     if (theWindow != NIL) {
  167.         wEditRec = NewEditRecord(theWindow);
  168.         wPort = theWindow->portRect;
  169.         r = wPort;
  170.         r.left = r.right-15;
  171.         r.bottom -=15;
  172.         r.right ++;
  173.         sBar = NewControl(theWindow,&r,NIL,TRUE,1,1,1,scrollBarProc,0L);
  174.         if (wEditRec != NIL)
  175.             (*wEditRec)->vScroll = sBar;
  176.         r = wPort;
  177.         r.top = r.bottom -15;
  178.         r.right -=15;
  179.         r.bottom++;
  180.         sBar = NewControl(theWindow,&r,NIL,TRUE,1,1,256,scrollBarProc,0L);
  181.         if (wEditRec != NIL)
  182.             (*wEditRec)->hScroll = sBar;
  183.         (*(WindowPeek)theWindow).windowKind = EditWindowKind;
  184.         InitEdDisplay(theWindow);
  185.     }
  186. }
  187.  
  188.  
  189. DisposeEditWindow(WindowPtr theWindow)
  190. {
  191.     /* removes all edit data structures, including linked list, edit record, window rec */
  192.     
  193.     textEdHdl    theTextRec;
  194.     
  195.     if (IsEditKind(theWindow)) {
  196.         theTextRec = GetWEditRecord(theWindow);
  197.         DisposeEdRecord(theTextRec);
  198.     }
  199.     DisposeWindow(theWindow);
  200. }
  201.  
  202.  
  203. GetScrollBars(WindowPtr theWindow,ControlHandle *v,ControlHandle *h)
  204. {
  205.     /* returns scroll bar handles from edit record */
  206.     
  207.     textEdHdl    theEd;
  208.     
  209.     if (IsEditKind(theWindow)) {
  210.         theEd = GetWEditRecord(theWindow);
  211.         if (theEd != NIL) {
  212.             *v = (*theEd)->vScroll;
  213.             *h = (*theEd)->hScroll;
  214.         }
  215.     }
  216. }
  217.  
  218.  
  219. ReCalcLineRange(WindowPtr theWindow)
  220. {
  221.     /* call whenever window is resized to recalculate the number of visible lines in the
  222.         window, and adjust scroll bar ranges accordingly */
  223.     
  224.     textEdHdl        theEd;
  225.     ControlHandle    vS,hS;
  226.     int                scMax;
  227.         
  228.     if (IsEditKind(theWindow)) {
  229.         theEd = GetWEditRecord(theWindow);
  230.         if (theEd != NIL) {
  231.             GetScrollBars(theWindow,&vS,&hS);
  232.             (*theEd)->nLines = GetNumberLines(theEd);
  233.             (*theEd)->linesInWindow = GetLinesFit(theWindow);
  234.             (*theEd)->charsAcross = GetHCharsFit(theWindow,(*theEd)->mSpace);
  235.             scMax = (*theEd)->nLines;
  236.             if (scMax < GetCtlMin(vS))
  237.                 scMax = GetCtlMin(vS);
  238.             SetCtlMax(vS,scMax);
  239.             (*theEd)->topLine = GetCtlValue(vS);
  240.  
  241.             SetCtlMax(hS,256 - (*theEd)->charsAcross);
  242.         }
  243.     }
  244. }
  245.  
  246.  
  247. MoveScrollbars(WindowPtr theWindow,ControlHandle vS,ControlHandle hS)
  248. {
  249.     /* code stub shared by ResizeWindow and ZoomScrollWindow to move the scrollbars to
  250.         the window edges. The window should have been set to the new size & bars hidden
  251.         before calling, and theWindow must be current port */
  252.     
  253.     Rect    tempRect;
  254.     
  255.     if (theWindow!=NIL && vS!=NIL && hS!=NIL){            
  256.         tempRect=(**vS).contrlRect;
  257.         tempRect.bottom+=15;                        /* erase the grow icon */
  258.         EraseRect(&tempRect);
  259.         InvalRect(&tempRect);
  260.         tempRect=(**hS).contrlRect;
  261.         EraseRect(&tempRect);
  262.         InvalRect(&tempRect);
  263.         tempRect=theWindow->portRect;
  264.                 
  265.         /* move the bars to the window edges, resizing if necessary */
  266.  
  267.         MoveControl(vS,tempRect.right-15,tempRect.top-1);
  268.         SizeControl(vS,16,tempRect.bottom-tempRect.top-13);
  269.         
  270.         MoveControl(hS,tempRect.left-1,tempRect.bottom-15);
  271.         SizeControl(hS,tempRect.right-tempRect.left-13,16);
  272.         
  273.         tempRect=(**vS).contrlRect;
  274.         ValidRect(&tempRect);
  275.         tempRect=(**hS).contrlRect;
  276.         ValidRect(&tempRect);
  277.         tempRect.right=tempRect.left;
  278.         tempRect.left=0;
  279.         EraseRect(&tempRect);
  280.         InvalRect(&tempRect);
  281.         ShowControl(vS);
  282.         ShowControl(hS);
  283.     }
  284. }
  285.  
  286.  
  287. ResizeEdWindow(WindowPtr theWindow,int width,int height)
  288. {
  289.     /* call in response to a grow or zoom request for the window- This routine moves the
  290.         scrollbars referenced by vS (vertical) and hS (horizontal), sorts out the various
  291.         update region manipulations required, and sets the clip region for the window to
  292.         exclude the scrollbars. It draws the controls as required, but removes them from
  293.         the update region to stop them being drawn twice. */
  294.         
  295.     Rect            tempRect;
  296.     GrafPtr            savePort;
  297.     ControlHandle    vS,hS;
  298.     int                vCheck;
  299.             
  300.     if (theWindow!=NIL) {
  301.         GetPort(&savePort);
  302.         SetPort(theWindow);
  303.         
  304.         if (IsEditKind(theWindow))
  305.             GetScrollBars(theWindow,&vS,&hS);
  306.             
  307.         vCheck = GetCtlValue(vS);
  308.         
  309.         /* first, hide the bars and mark their areas for update */
  310.  
  311.         if (vS!=NIL)
  312.             HideControl(vS);
  313.         if (hS!=NIL)
  314.             HideControl(hS);
  315.  
  316.         SizeWindow(theWindow,width,height,TRUE);
  317.         
  318.         MoveScrollbars(theWindow,vS,hS);
  319.         ReCalcLineRange(theWindow);    
  320.         DrawGrowIcon(theWindow);
  321.         if (vCheck != GetCtlValue(vS)) {
  322.             /* resizing altered the scroll position, so redraw the lot */
  323.             GetClipRect(theWindow,&tempRect);
  324.             EraseRect(&tempRect);
  325.             InvalRect(&tempRect);
  326.         }
  327.         SetPort(savePort);
  328.     }
  329. }
  330.  
  331.  
  332. ZoomEdScrollWindow(WindowPtr theWindow,int partCode,short front)
  333. {
  334.     /* replaces ZoomWindow function when window has scrollbars */
  335.     
  336.     ControlHandle    vS,hS;
  337.     
  338.     if (theWindow!=NIL && IsEditKind(theWindow)) {
  339.         GetScrollBars(theWindow,&vS,&hS);
  340.         if (vS!=NIL && hS!=NIL) {
  341.             HideControl(vS);
  342.             HideControl(hS);
  343.         }
  344.         ZoomWindow(theWindow,partCode,front);
  345.         MoveScrollbars(theWindow,vS,hS);
  346.         ReCalcLineRange(theWindow);
  347.         DrawGrowIcon(theWindow);
  348.     }
  349. }
  350.  
  351.  
  352. DisposeAllWindows(WindowPtr wListHead)
  353. {
  354.     /* calls disposeEdWindow for all windows in window list */
  355.     WindowPtr    w;
  356.     
  357.     while (wListHead != NIL) {
  358.         w = (*(WindowPeek)wListHead).nextWindow;
  359.         DisposeEditWindow(wListHead);
  360.         wListHead = w;
  361.     }
  362. }
  363.  
  364.  
  365. pascal void  ScrollProc(ControlHandle theControl,int partCode)
  366. {
  367.     /* action procedure for scrolling text in edit windows. To get text info, etc, the
  368.         text record is got from the control's owner window, so this applies ONLY to ed
  369.         windows */
  370.     
  371.     textEdHdl        theText;
  372.     ControlHandle    vS,hS;
  373.     int                lHeight;
  374.     int                amount,pageAmt;
  375.     WindowPtr        edWindow;
  376.     RgnHandle        upRgn,tempRgn;
  377.     Rect            sr;
  378.     
  379.     edWindow = (*theControl)->contrlOwner;
  380.     if (edWindow != NIL) {
  381.         theText = GetWEditRecord(edWindow);
  382.         GetScrollBars(edWindow,&vS,&hS);
  383.         if (theControl==vS) {
  384.             lHeight = GetLineHeight(edWindow);    /* vertical spacing */
  385.             pageAmt = (*theText)->linesInWindow -1;
  386.         }
  387.         else {
  388.             lHeight = (*theText)->mSpace;        /* horizontal spacing */
  389.             pageAmt = (*theText)->charsAcross/2;
  390.         }    
  391.     }
  392.     amount = GetCtlValue(theControl);
  393.     upRgn = NewRgn();
  394.     
  395.     switch (partCode) {
  396.         case inUpButton:
  397.             SetCtlValue(theControl,GetCtlValue(theControl)-1);
  398.             break;
  399.         case inDownButton:
  400.             SetCtlValue(theControl,GetCtlValue(theControl)+1);
  401.             break;
  402.         case inPageUp:
  403.             SetCtlValue(theControl,GetCtlValue(theControl)-pageAmt);
  404.             break;
  405.         case inPageDown:
  406.             SetCtlValue(theControl,GetCtlValue(theControl)+pageAmt);
  407.             break;
  408.     }
  409.     GetClipRect(edWindow,&sr);
  410.     if (theControl == vS) {
  411.         amount = (amount - GetCtlValue(theControl)) * lHeight;
  412.         if (amount != 0) {
  413.             ClipRect(&sr);
  414.             (*theText)->topLine = GetCtlValue(theControl);
  415.             ScrollRect(&sr,0,amount,upRgn);
  416.             DrawList(edWindow,upRgn);
  417.         }
  418.     }
  419.     else
  420.         if (theControl == hS) {
  421.             amount = (amount - GetCtlValue(theControl)) * lHeight;
  422.             if (amount != 0) {
  423.                 tempRgn = NewRgn();
  424.                 RectRgn(tempRgn,&sr);
  425.                 sr.top = thePort->portRect.top;
  426.                 ClipRect(&sr);
  427.                 ScrollRect(&sr,amount,0,upRgn);
  428.                 SectRgn(upRgn,tempRgn,tempRgn);
  429.                 DrawList(edWindow,tempRgn);
  430.                 sr.bottom = sr.top + GetTabBarHeight(theText) +2;
  431.                 RectRgn(tempRgn,&sr);
  432.                 SectRgn(upRgn,tempRgn,tempRgn);
  433.                 DrawTabBar(edWindow,tempRgn);
  434.                 DisposeRgn(tempRgn);
  435.             }
  436.         }
  437.     UnClipWindow(edWindow);
  438.     DisposeRgn(upRgn);
  439. }
  440.  
  441.  
  442. ClickEditWindow(WindowPtr theWindow,Point clickPt)
  443. {
  444.     /* handle clicks for edit window. Window is valid edit window, current port and click
  445.         is in local coords. All handed to you on a plate! */
  446.         
  447.     textEdHdl        theText;
  448.     ControlHandle    vS,hS,theControl;
  449.     int                partCode;
  450.     Rect            tr;
  451.     
  452.     theText = GetWEditRecord(theWindow);
  453.     if (theText != NIL) {
  454.         GetScrollBars(theWindow,&vS,&hS);
  455.         
  456.         partCode = FindControl(clickPt,theWindow,&theControl);
  457.         
  458.         if (partCode == inThumb) {
  459.             partCode = TrackControl(theControl,clickPt,NIL);
  460.             if (partCode != 0) {
  461.                 ReCalcLineRange(theWindow);
  462.                 GetClipRect(theWindow,&tr);
  463.                 if (theControl == hS)
  464.                     tr.top-= GetTabBarHeight(theText)+2;
  465.                 EraseRect(&tr);
  466.                 InvalRect(&tr);
  467.             }
  468.         }
  469.         else {
  470.             switch (partCode) {
  471.                 case inUpButton:
  472.                 case inDownButton:
  473.                 case inPageUp:
  474.                 case inPageDown:
  475.                     partCode = TrackControl(theControl,clickPt,&ScrollProc);
  476.                     ReCalcLineRange(theWindow);
  477.                     break;
  478.                 default:
  479.                     ClickText(theWindow,clickPt);
  480.                     break;
  481.             }
  482.         }
  483.     }
  484. }
  485.  
  486.  
  487. int ShowHideRulers(WindowPtr theWindow)
  488. {
  489.     /* toggles existence of ruler bar in window */
  490.     
  491.     textEdHdl    theText;
  492.     GrafPtr        savePort;
  493.     Rect        pr;
  494.     int            whichAction;
  495.     
  496.     if (IsEditKind(theWindow)) {
  497.         theText = GetWEditRecord(theWindow);
  498.         if (GetTabBarHeight(theText) == 0) {
  499.             SetTabBarHeight(theText,TabBarHeight);
  500.             whichAction = TRUE;
  501.         }
  502.         else {
  503.             SetTabBarHeight(theText,0);
  504.             whichAction = FALSE;
  505.         }
  506.             
  507.         GetPort(&savePort);
  508.         SetPort(theWindow);
  509.         pr = thePort->portRect;
  510.         pr.right -=15;
  511.         pr.bottom -= 15;
  512.         EraseRect(&pr);
  513.         InvalRect(&pr);
  514.         SetPort(savePort);
  515.         ReCalcLineRange(theWindow);
  516.         
  517.         return(whichAction);
  518.     }
  519. }
  520.  
  521.  
  522. OpenSourceCode(void)
  523. {
  524.     /* high level menu command to open a source file and display it in an edit window */
  525.  
  526.     WindowPtr    w;
  527.     OSErr        theErr;
  528.     SysEnvRec    macEnv;
  529.     SFReply        theReply;
  530.     
  531.     ChooseTextFile(&theReply);
  532.     if (theReply.good) {
  533.         theErr = SysEnvirons(2,&macEnv);
  534.         if (theErr == noErr && macEnv.hasColorQD)
  535.             w = GetNewCWindow(standardResID+1,NIL,(WindowPtr)-1L);
  536.         else
  537.             w = GetNewWindow(standardResID+1,NIL,(WindowPtr)-1L);
  538.             
  539.         if (w != NIL) {
  540.             ShowWindow(w);
  541.             InitEditWindow(w);
  542.             SelectWindow(w);
  543.             LoadText(&theReply,w);
  544.         }
  545.     }
  546. }
  547.